var express = require('express');
var router = express.Router();
const { loginService, getUserListService, userActionService } = require('../service/adminService')
const { formatResponse, analysisToken } = require('../utils/tool')

router.post('/login', async function(req, res, next) {
  const result = await loginService(req.body)
  if (result.token) {
    res.setHeader("Authorization", result.token)
    res.send(formatResponse(result, '登录成功'))
  } else {
    res.send(formatResponse(null, '账号或密码错误', 400))
  }
});

router.get('/logout', async function(req, res, next) {
  res.setHeader("Authorization", '')
  res.send(formatResponse(null))
})

router.get('/get_user_list', async function(req, res, next) {
  const result = await getUserListService(req.query)
	res.send(formatResponse(result))
})

const getUserInfo = (req) => {
  return analysisToken(req.get('Token'))
}

router.get('/user_action', async function(req, res, next) {
  const user = getUserInfo(req)
  await userActionService(req.query, user)
	res.send(formatResponse(null))
})



module.exports = router;
